package main

import (
	"fmt"
	"io/ioutil"
)

/**
 * 2.4条件语句
 * 1.if
 * (1).if的条件里不需要括号
 * (2).if的条件里可以赋值
 * (3).if的条件里赋值的变量作用域就在这个if语句里
 * 2.switch
 * (1) switch会自动break,除非使用fallthrough
 * (2) switch后可以没有表达式
 */
func main() {
	method1()
	method2()
	fmt.Println(
		grade(0),
		grade(59),
		grade(60),
		grade(82),
		grade(99),
		grade(100),
		grade(101),
		grade(-3),
	)
}

// 1.常规写法
func method1() {
	const filename = "abc.txt"
	contents, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s\n", contents)
	}
}

// 2.if可以像其他语言for写法
func method2() {
	const filename = "abc.txt"
	if contents, err := ioutil.ReadFile(filename); err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s\n", contents)
	}

}

// 3.switch
func grade(score int) string {
	g := ""
	switch {
	case score < 0 || score > 100:
		panic(fmt.Sprintf("Wrong score: %d", score))
	case score < 60:
		g = "F"
	case score < 80:
		g = "C"
	case score < 90:
		g = "B"
	case score <= 100:
		g = "A"
	default:
		panic(fmt.Sprintf("Wrong score: %d", score))
	}
	return g
}
